// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
    userMap.clear()
    const wxContext = cloud.getWXContext()
    const res = await db.collection('missionLog').where({
        missionId: event.missionId
    }).get();
    const history = res.data;
    for (const item of history) {
        item.userName = await getUserInfo(item.userId)
    }
    return history;

}
const userMap = new Map();
async function getUserInfo(id) {
    if (userMap.has(id)) return userMap.get(id);
    const res = await db.collection('user').doc(id).field({
        userName: true
    }).get()
    userMap.set(id, res.data.userName)
    return res.data.userName
}